package cn.bugDance.domain.credit.service.adjust;

import cn.bugDance.domain.credit.model.aggregate.CreditAdjustAggregate;
import cn.bugDance.domain.credit.model.entity.TaskEntity;
import cn.bugDance.domain.credit.model.entity.TradeEntity;
import cn.bugDance.domain.credit.model.entity.UserCreditAccountEntity;
import cn.bugDance.domain.credit.model.entity.UserCreditOrderEntity;
import cn.bugDance.domain.credit.model.valobj.CreditPayVO;
import cn.bugDance.domain.credit.model.valobj.TradeNameVO;
import cn.bugDance.domain.credit.model.valobj.TradeTypeVO;
import cn.bugDance.domain.credit.repository.ICreditRepository;
import cn.bugDance.domain.credit.service.ICreditAccountAdjustService;
import cn.bugDance.domain.credit.service.adjust.policy.IAdjustPolicy;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
@Slf4j
public class CreditAccountAdjustService implements ICreditAccountAdjustService {

    @Resource
    private ICreditRepository creditRepository;

    private Map<String, IAdjustPolicy> adjustPolicyGroup;

    public CreditAccountAdjustService(Map<String, IAdjustPolicy> adjustPolicyGroup) {
        this.adjustPolicyGroup = adjustPolicyGroup;
    }

    @Override
    public String createOrder(TradeEntity tradeEntity) {

        String userId = tradeEntity.getUserId();
        String bizId = tradeEntity.getBizId();
        Integer tradeAmount = tradeEntity.getTradeAmount();
        TradeNameVO tradeName  = tradeEntity.getTradeName();
        TradeTypeVO tradeType = tradeEntity.getTradeType();

        if(StringUtils.isBlank(userId) || StringUtils.isBlank(bizId) || tradeAmount == null || tradeName == null || tradeType == null){
            log.error("【CreditAccountAdjustService->】: 参数校验错误");
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        UserCreditAccountEntity userCreditAccountEntity = creditRepository.queryUserCreditAccount(userId);
        if(null == userCreditAccountEntity){
            throw new AppException(ResponseCode.NO_EXIST_CREDIT_ACCOUNT.getCode(), ResponseCode.NO_EXIST_CREDIT_ACCOUNT.getInfo());
        }
        if(userCreditAccountEntity.getAvailableAmount() < tradeAmount){
            throw new AppException(ResponseCode.AVAILABLE_AMOUNT_NO_ENOUGH.getCode(), ResponseCode.AVAILABLE_AMOUNT_NO_ENOUGH.getInfo());
        }

        IAdjustPolicy adjustPolicy = adjustPolicyGroup.get(tradeName.getCode());
        return adjustPolicy.adjust(tradeEntity);
    }

    @Override
    public Integer queryAvailableAmount(String userId) {
        UserCreditAccountEntity userCreditAccountEntity = creditRepository.queryUserCreditAccount(userId);
        if(null == userCreditAccountEntity){
            return 0;
        }
        return userCreditAccountEntity.getAvailableAmount();
    }
}
